﻿using AutoMapper;
using QYmall.Core.Model.Models;
using QYmall.Core.Model.ViewModels;

namespace QYmall.Core.Model.Profiles
{
    public class ArticleProfile : Profile
    {
        /// <summary>
        /// 内容实体映射
        /// </summary>
        public ArticleProfile()
        {

            //文章主表
            //源数据映射到DTO
            CreateMap<Articles, ArticlesDto>();
            CreateMap<Articles, ArticlesListDto>()
                .ForMember(
                    dest => dest.CategoryTitle,
                    opt =>
                    {
                        opt.MapFrom(src => string.Join(",", src.CategoryRelations.Select(x => x.ArticleCategory != null ? x.ArticleCategory.Title : String.Empty).ToArray()));
                    }
                );
            CreateMap<Articles, ArticlesClientDto>()
                .ForMember(
                    dest => dest.CategoryTitle,
                    opt =>
                    {
                        opt.MapFrom(src => string.Join(",", src.CategoryRelations.Select(x => x.ArticleCategory != null ? x.ArticleCategory.Title : String.Empty).ToArray()));
                    }
                );
            CreateMap<Articles, ArticlesEditDto>();
            //DTO映射到源数据
            CreateMap<ArticlesEditDto, Articles>();

            //文章分类
            //源数据映射到DTO
            CreateMap<ArticleCategory, ArticleCategoryDto>();
            CreateMap<ArticleCategory, ArticleCategoryEditDto>();
            //DTO映射到源数据
            CreateMap<ArticleCategoryEditDto, ArticleCategory>();

            //类别关系
            //源数据映射到DTO
            CreateMap<ArticleCategoryRelation, ArticleCategoryRelationDto>();
            //DTO映射到源数据
            CreateMap<ArticleCategoryRelationDto, ArticleCategoryRelation>();
        }
    }
}
